#region Using directives

using System;
using System.Drawing;
using System.Windows.Forms;

#endregion

namespace MobileConfig
{
	public class QuestionPanel: System.Windows.Forms.Panel
	{
		private Question _q = null;
		private System.Windows.Forms.Control _qCtl = null;
		private int _top = 0;

		public Question Question
		{
			get
			{
				return _q;
			}
		}

		public object SelectedValue
		{
			get
			{
				if (_qCtl != null)
				{
					if (_qCtl is TextBox)
						return _qCtl.Text;
					else if (_qCtl is ComboBox)
						return ((ComboBox)_qCtl).SelectedItem;
					else return null;
				}else
					return null;
			}
		}

		public QuestionPanel(Question Question)
		{
			_q = Question;

			if (_q != null)
			{
				SetDescription();
				SetQuestion();
				this.Height = _top;
			}
		}

		public void GiveFocus()
		{
			if (_qCtl != null)
			{
				_qCtl.BringToFront();
				_qCtl.Focus();
			}
		}

		private void SetDescription()
		{
			ResizeLabel rl = new ResizeLabel();
			rl.Text = _q.Description;
			rl.Location = new Point(0,_top);
			rl.Font  = new System.Drawing.Font("Segoe Condensed", 10F, System.Drawing.FontStyle.Bold);
			rl.Width = this.Width;
			this.Controls.Add(rl);
			_top += rl.Height + 6;			
		}

		private void SetQuestion()
		{
			if (_q.Values != null && (_q.Values.Length > 0 || _q.DefaultValue != null))
			{
				//textbox entry
				if (_q.Values.Length == 1 && _q.DefaultValue == null)
				{
					_qCtl = new System.Windows.Forms.TextBox();
					TextBox textCtl = (TextBox)_qCtl;

					if (_q.Values[0].RegValues[0].Value != null)
						textCtl.Text = _q.Values[0].RegValues[0].Value;
					else if (_q.Values[0].DefaultValue != null)
						textCtl.Text = _q.Values[0].DefaultValue;

					if (_q.Values[0].RegValues[0].Value == null)
					{
						RegValue rv = _q.Values[0].RegValues[0];
						RegistryKey regKey = Registry.UnknownKey(Utility.GetRootKey(rv.Key)).OpenSubKey(rv.SubKey);
						if (regKey != null)
						{
							object regVal = regKey.GetValue(rv.Name);
							if (regVal != null)
								textCtl.Text = regVal.ToString();
						}
					}
				}

				//combobox question
				if (_q.Values.Length > 1 || (_q.Values.Length == 1 && _q.DefaultValue != null))
				{
					_qCtl = new System.Windows.Forms.ComboBox();
					ComboBox comboCtl = (ComboBox)_qCtl;
					comboCtl.ValueMember = "ValueID";
					comboCtl.DisplayMember = "Name";

					foreach (Value v in _q.Values)
					{
						comboCtl.Items.Add(v);

						if (_q.DefaultValue != null && v.ID == _q.DefaultValue.ID)
							comboCtl.SelectedItem = v;
					}

					if (_q.SelectRegValue != null)
					{
						RegValue rv = _q.SelectRegValue;
						if (rv != null)
						{
							object selVal = Utility.GetRegKeyValue(Utility.GetRootKey(rv.Key), rv.SubKey, rv.Name, rv.Value);
							if (selVal != null)
							{
								foreach (Value v in comboCtl.Items)
								{
									if (v.value != null && v.value.ToLower() == selVal.ToString().ToLower())
									{
										comboCtl.SelectedItem = v;
										break;
									}
								}
							}
						}
					}
				}
				this.Controls.Add(_qCtl);
				_qCtl.Width = this.Width;
				_qCtl.Location = new Point(0, _top);				
				_top += _qCtl.Height;
			}
		}

		protected override void OnResize(EventArgs e)
		{
			base.OnResize(e);

			foreach (Control c in this.Controls)
			{
				c.Width = this.Width;
			}
		}
	}
}
